import { defineStore } from 'pinia'
import router from '@/router'
import { TOKEN, USER_INFO, STORAGE_KEY_NAV_TABS, SYS_TAG } from '@/constant'
import { loginOut, getUserInfo } from '../api/login'
import { ElLoading } from 'element-plus'

export const useUserStore = defineStore({
  id: 'user',

  state: () => ({
    userInfo: JSON.parse(localStorage.getItem(USER_INFO) || '{}') || {}
  }),

  actions: {
    login(data: any) {
      // 设置token缓存
      localStorage.setItem(TOKEN, data.token)
      // 调用getUserInfo获取用户信息及系统配置和权限信息
      getUserInfo({}).then((res: any) => {
        res = {
          userId: '1768220001239638017',
          userName: '郑文杰',
          password: null,
          phone: '13168365845',
          wechat: null,
          weibo: null,
          email: null,
          userType: 1000,
          userAvatar: null,
          userSkin: null,
          sort: null,
          remark: null,
          useStatus: null,
          createTime: '2024-03-14 18:17:55',
          updateTime: null,
          pwdUpdateUser: null,
          pwdUpdateTime: null,
          errorNum: null,
          errorTime: null,
          token: null,
          roleIdList: null,
          sysRoleEntityList: null,
          menus: [
            {
              menuId: '1761963863651127298',
              parentId: '0',
              menuPath: '/1761963863651127298',
              menuName: '系统管理',
              menuIcon: 'home',
              menuUrl: 'management',
              menuComponent: '#',
              menuRedirect: null,
              routeName: 'management',
              menuType: 1,
              remark: null,
              sort: 1,
              useStatus: null,
              createTime: null,
              updateTime: null,
              children: [
                {
                  menuId: '1787430971730149378',
                  parentId: '1761963863651127298',
                  menuPath: '/1761963863651127298/1787430971730149378',
                  menuName: '富文本测试',
                  menuIcon: 'side-home',
                  menuUrl: 'home',
                  menuComponent: 'home/index',
                  menuRedirect: null,
                  routeName: 'home',
                  menuType: 1,
                  remark: null,
                  sort: 1,
                  useStatus: null,
                  createTime: null,
                  updateTime: null,
                  children: []
                },
                {
                  menuId: '17874309717301493782',
                  parentId: '1761963863651127298',
                  menuPath: '/1761963863651127298/17874309717301493782',
                  menuName: '富文本测试2',
                  menuIcon: 'side-home',
                  menuUrl: 'home2',
                  menuComponent: 'home/index2',
                  menuRedirect: null,
                  routeName: 'home2',
                  menuType: 1,
                  remark: null,
                  sort: 1,
                  useStatus: null,
                  createTime: null,
                  updateTime: null,
                  children: []
                }
              ]
            }
          ],
          sliders: null,
          buttons: [],
          associationId: '1764573998051508227',
          roleName: null,
          identityList: [
            {
              identityId: 1000,
              identityName: '元谋人博物馆'
            }
          ]
        }
        // 设置用户信息缓存
        localStorage.setItem(USER_INFO, JSON.stringify(res))
        // 如果项目配置了系统标签，则将系统标签存于缓存
        const sysTag = import.meta.env.VITE_APP_SYSTAG
        if (sysTag) {
          localStorage.setItem(SYS_TAG, sysTag)
        }
        this.userInfo = res
        router.push('/')
      })
    },

    logout() {
      const loading = ElLoading.service({
        lock: true,
        fullscreen: true
      })
      loginOut &&
        loginOut()
          .then(() => {
            /** */
          })
          .finally(() => {
            loading.close()
            localStorage.removeItem(TOKEN)
            localStorage.removeItem(USER_INFO)
            localStorage.removeItem(SYS_TAG)
            sessionStorage.removeItem(STORAGE_KEY_NAV_TABS)
            router.push('/login')
          })
      !loginOut && loading.close()
    }
  }
})
